package com.apes.enquiry.orderForm.model;

import com.apes.enquiry.product.model.SpProduct;
import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.Date;
import java.util.Objects;

@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = OrderFormDetail.CONST_TABLE_NAME)
public class OrderFormDetail extends BaseModel {

    public static final String CONST_TABLE_NAME = "sp_order_form_detail";

    @Id
    @Column(name = "id", length = 10)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "订单明细", padding = 10)
    @Comment("ID")
    private String id; //明细ID

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "sp_id", referencedColumnName = "id")
    @Comment("商品列表")
    private SpProduct spProduct;

    @Column(name = "order_quantity", length = 11)
    @Comment("订单数量")
    private Integer orderQuantity=0;

    @Column(name = "termination_number", length = 11)
    @Comment("终止数量")
    private Integer terminationNumber=0;

    @Column(name = "termination_date")
    @Comment("终止时间")
    private Date terminationDate;

    @Column(name = "quantity_shipped", length = 11)
    @Comment("发货数量")
    private Integer quantityShipped=0;

    @Column(name = "AOG_number", length = 11)
    @Comment("到货数量")
    private Integer AOGNumber=0;

    @Column(name = "not_tax_unit_price", nullable = false, columnDefinition = "NUMBER(16,4) DEFAULT 0")
    @Comment("不含税单价")
    private Double notTaxUnitPrice = 0.0;

    @Column(name = "tax_unit_price", nullable = false, columnDefinition = "NUMBER(16,4) DEFAULT 0")
    @Comment("实际单价")
    private Double taxUnitPrice = 0.0;

    @Column(name = "third_party_coding", length = 11)
    @Comment("第三方编码")
    private String thirdPartyCoding;//ERP商品ID

    @Column(name = "baturu_order_no", length = 11)
    @Comment("巴图鲁订单编号")
    private String baturuOrderNo;

    @Column(name = "price_no", length = 50)
    @Comment("巴图鲁报价编号")
    private String priceNo;

    @Column(name = "enquiry_id", length = 11)
    @Comment("询价单号")
    private String enquiryId;//询价单号

    @Column(name = "state", length = 11)
    @Comment("货源状态")
    private String state;

    @Column(name = "product_item_id", length = 11)
    @Comment("要货单明细ID")
    private String productRequireItem;

    @Column(name = "enquiry_parts", length = 90)
    @Comment("询价配件")
    private String enquiryParts;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "order_form_id", referencedColumnName = "id")
    @Comment("订单列表")
    private OrderForm orderForm;

    @Transient
    private String brand;

    public String getBrand() {
        if (Objects.nonNull(spProduct) && this.spProduct.getSpBrand() != null) {
            return this.spProduct.getSpBrand().getName();
        }
        return null;
    }

    @Transient
    private String pn;

    public String getPn() {
        if (Objects.nonNull(spProduct) && this.spProduct.getOE() != null) {
            return this.spProduct.getOE();
        }
        return null;

    }

    @Transient
    private String merchantCoding;

    public String getMerchantCoding() {

        if (Objects.nonNull(spProduct) && this.spProduct.getMerchantProduct() != null) {
            return this.spProduct.getMerchantProduct();
        }
        return null;
    }


    @Transient
    private double subtotal;

    public double getSubtotal() {
        if(this.taxUnitPrice == null ) return 0;
        return Double.parseDouble(String.format("%.2f", (this.orderQuantity * this.taxUnitPrice)));
    }

    @Transient
    private boolean ticket;

    public boolean isTicket() {
        if(orderForm==null) return false;
        return orderForm.isInvoicing();
    }



}
